<?php
	require_once("db_connection.php");
	
	$queryType = $_POST["flag"];
	$data = array();
	
	if( $queryType == "country" ){
		$result = mysql_query("SELECT countryname, countrycode FROM country WHERE countryid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data = array(
				0 => $row['countryname'] ,
				1 => $row['countrycode']
			);
		}
	}else if( $queryType == "city" ){
		$result = mysql_query("SELECT cityname, countryid, zipcode FROM city WHERE cityid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data = array(
				0 => $row['cityname'] ,
				1 => $row['countryid'],
				2 => $row['zipcode']
			);
		}
	}else if( $queryType == "ralimo" ){
		$result = mysql_query("SELECT ra.id, ra.cityname, ra.vehicletype, ra.vehiclemodel, ra.capacity, ra.cancellationpolicy, ra.availability, rd.ratetype, rd.destination, rd.minimumhr, rd.currency, rd.rate, rs.remarks, rs.services FROM ralimorates ra LEFT JOIN ralimoratesdetail AS rd ON ra.id = rd.ralimorateid LEFT JOIN ralimorateservices AS rs ON ra.id = rs.ralimorateid WHERE ra.cityname = '$_POST[search]' ORDER BY ra.cityname");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$remarks = nl2br($row['remarks']);
			$remarks = str_replace("\r\n", "", $remarks);
			$remarks = str_replace("\n", "", $remarks);
			$remarks = str_replace("\r", "", $remarks);
			$remarks = str_replace("\t", "", $remarks);
			
			$services = nl2br($row['services']);
			$services = str_replace("\r\n", "", $services);
			$services = str_replace("\n", "", $services);
			$services = str_replace("\r", "", $services);
			$services = str_replace("\t", "", $services);
			
			$dest = '';
			if($row['ratetype'] == 'daily' ){
				if( $row['rate'] == 0){
				$rates = 'Upon Request';
					$dest = "Daily: " . $row['minimumhr'] . ": " .  $rates;
				}else{
					$dest = "Daily: " . $row['minimumhr'] . ": " .  $row['currency'] . " " . $row['rate'];
				}
			}else if($row['ratetype'] == 'hourly' ){
				if( $row['rate'] == 0){
					$rates = 'Rates Upon Request';
					$dest = "Hourly: " . $row['minimumhr'] . ": " .  $rates ."~";
				}else{
					$dest = "Hourly: " . $row['minimumhr'] . ": " .  $row['currency'] . " " . $row['rate'] ."~";
				}
			}else if($row['ratetype'] == 'uponrequest' ){
				if( $row['rate'] == 0){
					$rates = 'Rates Available Upon Request';
					$dest =   $rates ."~";
				}
			}else{
				if( $row['rate'] == 0){
				$rates = 'Upon Request';
					$dest = $row['destination']  . ": " . " " . $rates ;
				}else{
					$dest = $row['destination']  . ": " .  $row['currency']  . " " . $row['rate'];
				}
			}
			
			
			$data[] = array(
				0 => $row['id'] ,
				1 => $row['cityname'] ,
				2 => $row['vehicletype'] ,
				3 => $row['vehiclemodel'] ,
				4 => $row['capacity'] ,
				5 => $row['cancellationpolicy'] ,
				6 => $row['ratetype'],
				7 => $dest,
				8 => $remarks,
				9 => $services,
				10 => "|"
			);
		}
	}else if( $queryType == "ralimoothers" ){
		$result = mysql_query("SELECT * from ralimorateservices WHERE cityname = '$_POST[search]'");
		
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$remarks = nl2br($row['remarks']);
			$remarks = str_replace("\r\n", "", $remarks);
			$remarks = str_replace("\n", "", $remarks);
			$remarks = str_replace("\r", "", $remarks);
			$remarks = str_replace("\t", "", $remarks);
			
			$services = nl2br($row['services']);
			$services = str_replace("\r\n", "", $services);
			$services = str_replace("\n", "", $services);
			$services = str_replace("\r", "", $services);
			$services = str_replace("\t", "", $services);
			
			$data[] = array(
				0 => $remarks,
				1 => $services,
				2 => "|"
			);
		}
	}else if( $queryType == "ralimocapacity" ){
		$result = mysql_query("SELECT * from ralimorates WHERE cityname = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			
			$data[] = array(
				0 => $row['id'] ,
				1 => $row['vehiclemodel'] ,
				2 => $row['capacity'] ,
				3 => $row['cancellationpolicy'] ,
				4 => "|"
			);
		}
	}else if( $queryType == "affiliate" ){
		$result = mysql_query("SELECT * FROM affiliate WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data = array(
				0 => $row['companyname'] ,
				1 => $row['companyaddress'],
				2 => $row['companyemail'],
				3 => $row['landline'],
				4 => $row['faxno'],
				5 => $row['mobileno1'],
				6 => $row['cityid'],
				7 => $row['websiteurl'],
				8 => $row['contactname'],
				9 => $row['contactemail']
			);
		}
	}else if( $queryType == "afcity" ){
		$result = mysql_query("SELECT * FROM affiliate WHERE cityid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data = array(
				0 => $row['affiliateid'] ,
				1 => $row['companyname'] ,
				2 => $row['companyaddress'],
				3 => $row['companyemail'],
				4 => $row['landline'],
				5 => $row['faxno'],
				6 => $row['mobileno1'],
				7 => $row['cityid'],
				8 => $row['websiteurl'],
				9 => $row['contactname'],
				10 => $row['contactemail']
			);
		}
	}else if( $queryType == "afoneway" ){
		$result = mysql_query("SELECT * FROM onewayrate WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['onewayid'] ,
				1 => $row['vehicletype'] ,
				2 => $row['vehiclemodel'],
				3 => $row['distance'],
				4 => $row['currency'],
				5 => $row['rate'],
				6 => '|'
			);
		}
	}else if( $queryType == "afhourly" ){
		$result = mysql_query("SELECT * FROM hourlyrate WHERE affiliateid = '$_POST[search]' ORDER BY rate, minimumhr");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['hourlyid'] ,
				1 => $row['vehicletype'] ,
				2 => $row['vehiclemodel'],
				3 => $row['minimumhr'],
				4 => $row['currency'],
				5 => $row['rate'],
				6 => '|'
			);
		}
	}else if( $queryType == "afairport" ){
		$result = mysql_query("SELECT * FROM airportrate WHERE affiliateid = '$_POST[search]' ORDER BY vehiclemodel, destination");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['airrateid'] ,
				1 => $row['vehicletype'] ,
				2 => $row['vehiclemodel'],
				3 => $row['destination'],
				4 => $row['currency'],
				5 => $row['rate'],
				6 => '|'
			);
		}
	}else if( $queryType == "afnotes" ){
		$result = mysql_query("SELECT * FROM afremarks WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$remarks = nl2br($row['remarks']);
			$remarks = str_replace("\r\n", "", $remarks);
			$remarks = str_replace("\n", "", $remarks);
			$remarks = str_replace("\r", "", $remarks);
			$remarks = str_replace("\t", "", $remarks);
			$data[] = array(
				0 => $remarks
			);
		}
	}else if( $queryType == "afservices" ){
		$result = mysql_query("SELECT * FROM afservices WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$services = nl2br($row['services']);
			$services = str_replace("\r\n", "", $services);
			$services = str_replace("\n", "", $services);
			$services = str_replace("\r", "", $services);
			$services = str_replace("\t", "", $services);
			$data[] = array(
				0 => $services
			);
		}
	}else if( $queryType == "affile" ){
		$result = mysql_query("SELECT * FROM affile WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['filepath'],
				1 => '|'
			);
		}
	}else if( $queryType == "afvehicle" ){
		$result = mysql_query("SELECT * FROM vehicles WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['vehicleid'],
				1 => $row['vehicletype'],
				2 => $row['vehiclemodel'],
				3 => $row['capacity'],
				4 => $row['image'],
				5 => "|"
			);
		}
	}else if( $queryType == "afdriver" ){
		$result = mysql_query("SELECT * FROM drivers WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['driverid'],
				1 => $row['driversname'],
				2 => $row['contactno'],
				3 => $row['image'],
				4 => "|"
			);
		}
	}else if( $queryType == "aftoc" ){
		$result = mysql_query("SELECT * FROM afterms WHERE affiliateid = '$_POST[search]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$toc = nl2br($row['termsandconditions']);
			$toc = str_replace("\r\n", "", $toc);
			$toc = str_replace("\n", "", $toc);
			$toc = str_replace("\r", "", $toc);
			$toc = str_replace("\t", "", $toc);
			$data[] = array(
				0 => $toc
			);
		}
	}
	
	echo json_encode($data);
	flush();
?>